<template>
  <div>
    <!-- 搜索栏对象 -->
    <el-form ref="form" :model="searchForm" :inline="true">
      <el-form-item>
        <el-input v-model="searchForm.Comment" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.Comment" placeholder="请输入影片名"></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.Comment" placeholder="请输入评论内容"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="searchCommentInfo">搜索</el-button>
        <el-button type="primary" style="margin-right: 10px;">清空</el-button>
        <el-popconfirm title="确认要删除该评论吗？" @confirm="deleteComment">
          <el-button slot="reference" type="danger" :disabled="deleteAllBtnStu">批量删除</el-button>
        </el-popconfirm>
      </el-form-item>
    </el-form>

    <el-table :data="tableData" border style="width: 100%; margin-bottom: 20px;" :cell-style="{ padding: '2px' }"
              @selection-change="selectionChange">
      <el-table-column type="selection" width="50">
      </el-table-column>
      <el-table-column label="评论编号" width="100" prop="id">
      </el-table-column>
      <el-table-column label="电影编号" width="100" prop="movieNum">
      </el-table-column>
      <el-table-column label="电影名称" width="150" prop="movieName">
      </el-table-column>
      <el-table-column label="手机号" width="150" prop="tel">
      </el-table-column>
      <el-table-column label="用户名" width="80" prop="username">
      </el-table-column>
      <el-table-column label="评分" width="150">
        <template slot-scope="scope">
          <el-rate v-model=scope.row.value disabled show-score text-color="#ff9900"
                   score-template="{scope.row.value}">
          </el-rate>
        </template>
      </el-table-column>
      <el-table-column label="评论内容" prop="content">
      </el-table-column>

      <el-table-column label="添加时间" width="200">
        <template slot-scope="scope">
          {{ scope.row.created | fmtDate }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="60">
        <template slot-scope="scope">
          <el-popconfirm title="确认要删除该用户信息吗？" @confirm="deleteComment(scope.row.id)">
            <el-button slot="reference" type="text">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 显示分页页码 -->
    <el-pagination background layout="total,prev,pager,next,jumper" :total="total" :page-size="size"
                   :page-sizes="[5, 10, 15, 20, 25]" :current-page=current @current-change="loadComment">
    </el-pagination>

    <el-dialog title="评论信息" :visible.sync="dialogFormVisible" width="600px" :before-close="handleClose">
      <el-form :model="CommentForm" ref="CommentForm" label-width="100px" :rules="rules">
        <el-form-item label="电影名" label-width="100px" prop="movieName">
          <el-input v-model="CommentForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="评论者" label-width="100px" prop="username">
          <el-input v-model="CommentForm.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="评论" label-width="100px" prop="content">
          <el-input v-model="CommentForm.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="评分" label-width="100px" prop="value">
          <el-input v-model="CommentForm.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话" label-width="100px" prop="tel">
          <el-input v-model="CommentForm.content" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('FanForm')">保存</el-button>
          <el-button @click="resetForm('FanForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>

export default {
  data() {
    return {
      total: 10,
      size: 5,
      tableData: [],
      current: 1,
      deleteAllBtnStu: true,
      searchForm: {
        Comment: '',
      },
      CommentForm: {},
      dialogFormVisible: false,
      rules: {
        movieName: [
          {required: true, message: '请输入电影名称', trigger: 'blur'}
        ],
        username: [
          {required: true, message: '请输入评论者名称', trigger: 'blur'}
        ],
        content: [
          {required: true, message: '请输入评论', trigger: 'blur'},

        ],
      },
    }
  },
  filters: {
    fmtDate: (value) => {
      let date = new Date(value)
      let y = date.getFullYear()
      let M = (date.getMonth() + 1).toString().padStart(2, '0')
      let d = date.getDate().toString().padStart(2, '0')

      let h = date.getHours().toString().padStart(2, '0')
      let m = date.getMinutes().toString().padStart(2, '0')
      let s = date.getSeconds().toString().padStart(2, '0')

      return `${y}年${M}月${d}日 ${h}:${m}:${s}`
    }
  },
  created() {
    this.loadComment(this.current)
  },
  methods: {
    loadComment(val) {
      this.tableData = []
      this.$axios.get('/system/comment/list', {})
          .then(response => {
            for (let i = (val - 1) * response.data.data.size; i < val * response.data.data.size; i++) {
              this.tableData.push(response.data.data.records[i])
              console.log(i);
            }
            this.current = val
            this.total = response.data.data.total
            this.size = response.data.data.size
          })
    },
    selectionChange(value) {
      this.selection = value //selection数组中存放的就是选中所有的用户对象数据
      if (this.selection) {
        //如果seleciton数组中有数据，代表页面上是有选中的用户的，就修改批量删除为可用
        this.deleteAllBtnStu = (this.selection.length === 0) //直接给false，或者判断数组如果长度为0，表示没有数据，赋值true(表示不可用)
      }
    },
    handleClose() {
      this.resetForm('CommentForm')
      this.dialogFormVisible = false
    },
    loadCommentList() {
      this.$axios.get('/system/comment/list', {
        current: this.current, //要请求显示第几页 2
        size: this.size,//请求一页显示多少条 5
      })
          .then(response => {
            this.tableData = response.data.data.records
            this.total = response.data.data.total
            this.size = response.data.data.size
          })
    },
    deleteComment(id) {
      //单个删除用户和批量删除用户
      //如果id有值，表示单个删除，如果id没有值，表示进行批量删除(批量删除用户数据 this.selection)
      let ids = [] // 表示要删除的一组用户的id
      id ? ids.push(id) : this.selection.forEach(u => {
        ids.push(u.id)
      })
      //ids存储就是单个删除的用户ID，或者批量删除的用户ID
      this.$axios.post('/system/comment/delete', ids).then(response => {
        this.$message({
          message: response.data.message,
          type: 'success',
          showClose: true,
          //定义一个关闭消息框回调，需要重新加载用户的数据
          onClose: () => {
            this.loadCommentList()
          }
        })
      })

    },
    searchCommentInfo() {

    },
    submitForm(formName) {
      this.$refs.CommentForm.validate(valid => {
        if (valid) {
          //提交保存新建用户数据：不包括用户头像
          this.$axios.post('/system/comment/' + (this.CommentForm.id ? 'update' : 'save'), this.CommentForm)
              .then(response => {
                this.$message({
                  message: response.data.message,
                  type: 'success',
                  duration: 1200,
                  // showClose:true,
                  onClose: () => {
                    this.loadCommentList() //新建用户成功后，需要重新加载用户数据
                    this.dialogFormVisible = false //用户保存成功，对话框隐藏
                    this.resetForm(formName) //用户保存成功，表单重置
                  }
                })
              })
        }
      })
    },
    resetForm(formName) {
      this.CommentForm = []
      this.$refs[formName].resetFields()
    },
  },
  components: {}
}
</script>
<style scoped>

.el-table__body {
  font-size: 14px;
}

.el-button:hover {
  color: #333
}

.el-popconfirm-reference {
  cursor: pointer;
}

.el-pagination {
  display: flex;
  justify-content: flex-end;
}

.el-tag {
  font-size: 12px;
  padding: 2px 6px;
}

.el-table-column--selection {
  text-align: center;
}

.el-avatar {
  border-radius: 50%;
}

.el-divider {
  margin: 0 6px;
}

/*上传头像*/
/*.avatar-uploader {*/
/*  border: 1px dashed #d9d9d9;*/
/*  border-radius: 6px;*/
/*  cursor: pointer;*/
/*  position: relative;*/
/*  overflow: hidden;*/
/*}*/

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-dialog {
  text-align: center;
}

/* Style the dialog header */
.el-dialog__header {
  background-color: #2f4050;
  color: #fff;
  border-radius: 4px 4px 0 0;
  border-bottom: none;
}

/* Style the dialog title */
.el-dialog__title {
  font-size: 22px;
  font-weight: bold;
  padding: 20px;
}

/* Style the form labels */
.el-form-item__label {
  font-weight: bold;
  font-size: 16px;
  color: #333;
}

/* Style the input fields */
.el-input__inner {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  color: #606266;
}

/* Style the radio buttons */


.el-radio-button {
  flex: 1;
}

.dialog-footer {
  padding: 20px;
  text-align: right;
  border-top: 1px solid #ebeef5;
  border-radius: 0 0 4px 4px;
}

.dialog-footer .el-button {
  margin-left: 10px;
  border-radius: 4px;
}

.dialog-footer .el-button--primary {
  background-color: #409EFF;
  border-color: #409EFF;
}

.dialog-footer .el-button--primary:hover {
  background-color: #66b1ff;
  border-color: #66b1ff;
}

.dialog-footer .el-button--primary:active {
  background-color: #3a8ee6;
  border-color: #3a8ee6;
}

.dialog-footer .el-button--default {
  color: #333;
  border-color: #dcdfe6;
}

.dialog-footer .el-button--default:hover {
  color: #409EFF;
  border-color: #409EFF;
}
</style>